package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.ProductCatalog;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IProductCatalogService;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author mycp
 * 积分商城商品分类处理器
 */
@Controller
@RequestMapping("/productCatalog")
public class ProductCatalogController {

    @Autowired
    private IProductCatalogService productCatalogService;

    /**
     * 分页查询
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        //查询所有的积分商品分类
        model.addAttribute("pageInfo", productCatalogService.query(qo));
        return "productCatalog/list";
    }

    /**
     * 获取商品分类内容的接口
     * @param id
     * @return
     */
    @RequestMapping("/getContent")
    @ResponseBody
    public Object getContent(Long id) {
        //return productCatalogService.getContentById(id);
        return null;
    }

    /**
     * 增加/修改
     * @param productCatalog
     * @param img
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(ProductCatalog productCatalog, MultipartFile img) {
        //判断文件是否有值
        if (img != null && img.getContentType().startsWith("image") && img.getSize() > 0) {
            String url = UploadUtil.upload(img);
            productCatalog.setCoverUrl(url);
        }
        productCatalogService.saveorUpdate(productCatalog);
        return "redirect:/productCatalog/list.do";
    }

}
